package com.example.demo;

import com.example.demo.aop.ICalculator;
import com.example.demo.config.artemis.ActiveMqManager;
import com.example.demo.spring.AdminUser;
import com.example.demo.spring.RolesManager;
import com.example.demo.spring.SpringBeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.h2.H2ConsoleAutoConfiguration;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.jms.annotation.EnableJms;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

@SpringBootApplication(exclude = {H2ConsoleAutoConfiguration.class})
@ServletComponentScan
@EnableJms
public class DemoApplication {
	@Autowired
	private RolesManager rolesManager;

	@Autowired
	private ICalculator calculator;

	@Autowired
	private DataSource dataSource;

	public static void main(String[] args) {
		System.setProperty("hawtio.authenticationEnabled", "false");
		SpringApplication.run(DemoApplication.class, args);

		Object adminUser = SpringBeanUtils.getBean("adminUser");
		if(adminUser instanceof AdminUser) {

		}

		Object obj = SpringBeanUtils.getBean(DemoApplication.class);

		if(obj instanceof  DemoApplication) {
			DemoApplication app = (DemoApplication) obj;

		}

		RolesManager role = SpringBeanUtils.getBean(RolesManager.class);

		if(role instanceof  RolesManager) {
			RolesManager manager = (RolesManager) role;

		}

	}

	@PostConstruct
	public void postRun() {
		this.rolesManager.rolesManage();
		//calculator.add(12d, 14d);

		calculator.add(-4d, -455d);

		try {
			Connection conn = dataSource.getConnection();
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery("select * from test limit 1;");
			while(rs.next()) {
				System.out.println(rs.getInt("id") + " : " + rs.getString("name"));
			}

			conn.close();

		} catch (SQLException e) {

		}

	}

}
